package com.zch.jh.service.impl;

import com.zch.jh.service.LogisticService;
import com.zch.jh.domain.Logistic;
import com.zch.jh.repository.LogisticRepository;
import com.zch.jh.service.dto.LogisticDTO;
import com.zch.jh.service.mapper.LogisticMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing {@link Logistic}.
 */
@Service
@Transactional
public class LogisticServiceImpl implements LogisticService {

    private final Logger log = LoggerFactory.getLogger(LogisticServiceImpl.class);

    private final LogisticRepository logisticRepository;

    private final LogisticMapper logisticMapper;

    public LogisticServiceImpl(LogisticRepository logisticRepository, LogisticMapper logisticMapper) {
        this.logisticRepository = logisticRepository;
        this.logisticMapper = logisticMapper;
    }

    @Override
    public LogisticDTO save(LogisticDTO logisticDTO) {
        log.debug("Request to save Logistic : {}", logisticDTO);
        Logistic logistic = logisticMapper.toEntity(logisticDTO);
        logistic = logisticRepository.save(logistic);
        return logisticMapper.toDto(logistic);
    }

    @Override
    @Transactional(readOnly = true)
    public List<LogisticDTO> findAll() {
        log.debug("Request to get all Logistics");
        return logisticRepository.findAll().stream()
            .map(logisticMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }


    @Override
    @Transactional(readOnly = true)
    public Optional<LogisticDTO> findOne(Long id) {
        log.debug("Request to get Logistic : {}", id);
        return logisticRepository.findById(id)
            .map(logisticMapper::toDto);
    }

    @Override
    public void delete(Long id) {
        log.debug("Request to delete Logistic : {}", id);
        logisticRepository.deleteById(id);
    }
}
